/*
 * Copyright 2009 Douglas Lockamy, dlockamy_at_gmail.com
 * All rights reserved. Distributed under the terms of the MIT License.
 */
if(!debug)
    var debug = 0;

//Default colors as set by Default Theme. If not using defualt theme, set colors here
var mainBackgroundColor = "#ffffff";//defaultBackgroundColor ;
var mainViewColor =  "#999999";
var defaultWindowBarColor = "#ffff00";
var defaultViewColor = "#555555";

var GKversionBanner = "WindowKit.js version 1<br>(c)2008-2011 Douglas J Lockamy" ;

/*
	Do not edit past this point
*/


var xShift = 0;
var yShift = 0;

var isDragging = 0;
var topWindow = 0;

var draggingX = 0;
var draggingY = 0;

var mouseDownX = 0;
var mouseDownY = 0;

function selected(f)
{
    //printf("selected");
};

var windowDown = function(windowID,e)
{
	isDragging = 1;	
	
	topWindow = windowID;
	
	mouseDownX = e.pageX;
	mouseDownY = e.pageY;
   
	xShift = mouseDownX - topWindow.style.left;
	yShift = mouseDownY - topWindow.style.top;   
};

var windowMove = function(e)
{
   if(isDragging)
   {
        currentX = e.pageX;
        currentY = e.pageY;
        
        var windowBar = document.getElementById(topWindow.id);       

        changeX = currentX - mouseDownX;
        changeY = currentY - mouseDownY;
 
        var newX = currentX - xShift;
        var newY = currentY - yShift;
                
        if(newX > 0 && newY > 0)
        {   
            windowBar.style.left = newX+"px";
            windowBar.style.top = newY+"px";
        }
   }
   else
        return;
};

var windowUp = function()
{
    isDragging = 0;
};

Object = function()
{
    create = function()
    {
    
    
    };
};

Screen = function(type)
{
    this.height;
    this.width;
	
	this.getWidth = function()
	{
		return this.width;
	};
	
	this.setFont = function()
	{
		
	};
	this.getFontSize = function()
	{
		
	}
}; 

var CGA = new Screen(320,200);
var QVGA = new Screen(320,240);
var WVGA = new Screen(640,480);
var WSVGA = new Screen(1024,600);
var PAL = new Screen(720,576);
var SVGA = new Screen(800,600);
var XGA = new Screen(1024,768);
var WXGA = new Screen(1280,720);
var HD = new Screen(1920,1080);

BFrame = function(x1,y1,h,w)
{
    this.x = x1;
    this.y = y1;
    this.height = h;
    this.width = w;
};

BRect = function(x1,y1,h,w)
{
    this.x = x1;
    this.y = y1;
    this.height = h;
    this.width = w;
};

BPoint = function(x,y)
{
    this.x = x;
    this.y = y;
};

BShape = function()
{
 
};

BPolygon = function()
{

};

var checkFlags = function(flags,string)
{
	if(flags == "NO_TITLE")
		return 1;
	return 0;
};

var windowKill = function( windowID )
{
	var thisNode = document.getElementById(windowID.id);
	thisNode.parentNode.removeChild(thisNode);
};

BDirectWindow = function(frame,title,type,flags,screen)
{
    this.dragDown = 0;
    this.AVOID_FRONT = 0;

	if(checkFlags(flags,"NO_TITLE"))
		this.TITLED_WINDOW = 0;
	else
		this.TITLED_WINDOW = 1;
	
    this.mainView = 0;
	this.name = title;
    var d = new Date();
    this.myName = title+d.getTime(); 
    this.newWindow = document.createElement('div');
    this.newWindow.id = this.myName ;
	this.end = 0;
//default window style

    this.newWindow.style.position = "absolute"; 
    this.newWindow.style.overflow = "hidden";   
    this.newWindow.style.left = frame.x;
    this.newWindow.style.top = frame.y;
    this.newWindow.style.width = frame.width;
    this.newWindow.style.height = frame.height+25;    
    this.newWindow.style.color = "white";
    this.newWindow.style.visibility="hidden";   
    
    this.newWindow.setAttribute("class", "fatty");
    
    if(this.TITLED_WINDOW && flags != "TRAY_VIEW" )
    {
        this.titleBar = document.createElement('div');
        this.titleBar.id = this.myName;
        this.titleBar.innerHTML = "<img width=\"10px\" src=\""+rootPath+"/system/themes/glass/icons/minus.png\" onclick=\"windowKill(this.parentNode)\">  "+title;
        this.titleBar.style.height = "25px";
        this.titleBar.style.backgroundColor = defaultWindowBarColor;
        this.titleBar.style.border = "inset red 4px";
        this.titleBar.style.borderColor = "black" ;    
        this.titleBar.style.borderWidth = "0px 0px 1px 0px";  
        this.titleBar.onselect = 0;  
        this.titleBar.onmousedown = function(e) {
            windowDown(this,e);
        }
        this.titleBar.onmousemove = windowMove;
        this.titleBar.onmouseup = function() {
            windowUp();
        }
        this.newWindow.appendChild(this.titleBar); 
		
		this.mainViewFrame = new BFrame(0,0,frame.height+25,frame.width);
		this.mainView = new BDirectView(this.mainViewFrame,"aView","aView","aView",0);
        this.newWindow.appendChild(this.mainView.myView);
        this.newWindow.onselect = 0;
    }
    if(flags == "TRAY_VIEW" )
    {
        this.mainView = document.createElement('div');
        this.mainView.id = this.myName;
        this.mainView.innerHTML = "";
        this.mainView.style.top = 0;
		this.mainView.style.height = "25";
        this.mainView.style.backgroundColor = "b7b6b6";
        this.newWindow.appendChild(this.mainView);
        this.newWindow.onselect = 0;		
	}
	
    if(!this.TITLED_WINDOW && flags != "TRAY_VIEW")
    {        
        this.mainView = document.createElement('div');
        this.mainView.id = this.myName;
        this.mainView.innerHTML = "";
        this.mainView.style.top = "3";
		this.mainView.setAttribute('class','fattyView');

        this.newWindow.appendChild(this.mainView);
        this.newWindow.onselect = 0;
    }   
	
    if(this.AVOID_FRONT)
        this.newWindow.style.zIndex = -2;    
    
    document.body.appendChild(this.newWindow);      
    
    this.input = function(keyCode)
    {
    
    
    };

	this.hide = function()
	{
		this.newWindow.style.visibility="hidden";		
	};
	
    this.setTop = function()
    {
		if(this.TITLED_WINDOW)
		{
			this.titleBar.style.backgroundColor = "809090";			
		};
        
    };

	
    this.setNotTop = function()
    {
		if(this.TITLED_WINDOW)
		{
			this.titleBar.style.backgroundColor = "607070";			
		};   
    };
    
    this.AddChild = function(aView)
    {
        //if(aView)
          //  this.newWindow.appendChild(aView);  
        //else
          //  return -1;
    };
    this.addChildDiv = function(divName)
    {
        if(document.getElementById(this.myName))
            document.getElementById(this.myName).appendChild(divName);
        else
            return 'Bad Window Name';
    }

    this.addiFrame = function(url)
    {

	return;
    };
    
    this.getID = function()
    {
        return this.myName;
    };
    
	this.getView = function()
	{
		alert(this.mainView);
        return this.mainView;		
	};
	
    this.deleteWin = function()
    {
        var rmWin = document.getElementById(this.myName);
        rmWin.parentNode.removeChild(rmWin);        
    }

    this.Show = function()
    {
        this.newWindow.style.visibility="visible";
    };

    this.bounds = function()
    {
    
    }
    
};

BDirectView = function(frame,title,type,flags) 
{
    if(!frame)
        return -1;

    var d = new Date();
	this.myName = title+d.getTime();
    
    this.myFrame = frame;
	
    this.myView = document.createElement('div');
    this.myView.id = this.myName ;
    this.myView.style.position = "absolute"; 
    this.myView.style.overflow = "hidden";   
    this.myView.style.left = this.myFrame.x;
    this.myView.style.top = this.myFrame.y;
    this.myView.style.bottom = this.myFrame.width;
    this.myView.style.width = this.myFrame.width;
    this.myView.style.height = this.myFrame.height;    
    this.myView.style.color = "white";
    this.myView.style.backgroundColor = defaultViewColor;
    this.myView.style.visibility="visible";
    //this.myView.style.zIndex="3";
	this.myView.style.position = "relative";
	this.myView.innerHTML = "ddd";

    this.myName = title+d.getTime(),

    this.getView = function()
    {       
        return newView;
    }
          
    this.getViewId = function()
    {       
        return newView.id;
    }

    this.AddChild = function(aview)
    {
        if(newView)
            this.newView.appendChild(aView);    
        else
            return -1;  
    },
 
    this.Show = function()
    {
        newView.style.visibility="visible";    
    }

    this.Hide = function()
    {
        newView.style.visibility="hidden";    
    }

    this.SetHighColor = function(color)
    {
        newView.style.backgroundColor = color;
    }

    this.SetLowColor = function(color)
    {
        newView.style.backgroundColor = color;
    }
          
    this.MoveBy = function(x,y)
    {
        myFrame.x = myFrame.x - x;
        myFrame.y = myFrame.y - y;
        newView.style.left = myFrame.x;
        newView.style.top = myFrame.y;    
    }
 
    this.ReSizeBy = function(height,width)
    {   
        myFrame.height = myFrame.height - height;
        myFrame.width = myFrame.width - width;
        newView.style.width = myFrame.width;
        newView.style.height = myFrame.height;   
    }
    
    this.MoveTo = function(x,y)
    {
        myFrame.x = x;
        myFrame.y = y;
    
        newView.style.left = myFrame.x;
        newView.style.top = myFrame.y;        
    }
    
    this.ReSizeTo = function(height,width)
    {   
        myFrame.height = height;
        myFrame.width = width;
        newView.style.width = myFrame.width;
        newView.style.height = myFrame.height;   
    }

    this.Bounds = function()
    {
        return this.myFrame;
    }
    
    this.KeyDown = function(keyCode)
    {
        alert("you pressed "+keyCode);
    }
    
    this.KeyUp = function(keyCode)
    {
        alert("you released "+keyCode);
    } 
};

BAnimation = function(aView,animationType,lengthInSeconds,flags)
{
    isRunning = 0;
    this.stopping = 0;
    runTime = lengthInSeconds;
    this.frame = 0;
    timer = 0;
    effectType = animationType;
    frame = 0;
    isRunning = 0;
    endX = flags.x;
    endY = flags.y;
    
    viewCurrent = aView.Bounds();

    stepX = (viewCurrent.x - flags.x ) / lengthInSeconds;
    stepY =  (viewCurrent.y - flags.y) / lengthInSeconds;
    
    shrinkX = (viewCurrent.width - flags.width) / lengthInSeconds;
    shrinkY = (viewCurrent.height - flags.height) /lengthInSeconds; 
    
    this.tick = function()
    {
        if(isRunning && frame <= runTime )
        {
            frame++;
            if(effectType=="SHRINK")
            {
                aView.MoveBy(stepX,stepY);
                aView.ReSizeBy(shrinkX,shrinkY);   
            }
        }
        else
        {
            clearInterval(timer);
        }
    };

    this.Play = function()
    {
        if(!this.isRunning)
        {
            isRunning = 1;
            timer = setInterval(this.tick,20); 
        }  
    };
    
    this.Pause = function()
    {
         this.isRunning = false;   
    };
    
    this.Stop = function()
    {
        this.isRunning = false;
        this.stopping = true;
   
    };

};

//printf(GKversionBanner);
